package com.ttsx.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ttsx.core.common.exception.TTSXException;
import com.ttsx.mapper.UserMapper;
import com.ttsx.pojo.converter.UserConverter;
import com.ttsx.pojo.dto.user.UserLoginDTO;
import com.ttsx.pojo.dto.user.UserQueryDTO;
import com.ttsx.pojo.dto.user.UserRegisterDTO;
import com.ttsx.pojo.entity.User;
import com.ttsx.service.CartService;
import com.ttsx.service.UserService;
import com.ttsx.core.common.result.ResultCodeEnum;
import com.ttsx.core.common.util.WebUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lucky
 * @description 针对表【ttsx_user(用户表)】的数据库操作Service实现
 * @createDate 2025-03-01 17:41:06
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private CartService cartService;

    @Override
    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        User user = baseMapper.selectOne(new LambdaQueryWrapper<User>().eq(User::getUsername, username));

        if (user == null) {
            throw new TTSXException(ResultCodeEnum.USERNAME_PASSWORD_ERROR);
        }

        if (!DigestUtil.md5Hex(password).equals(user.getPassword())) {
            throw new TTSXException(ResultCodeEnum.USERNAME_PASSWORD_ERROR);
        }

        user.setCartCount(cartService.getCartCount(user.getId()));

        WebUtils.getRequest().getSession().setAttribute("user", user);

        return user;
    }

    @Override
    public Boolean register(UserRegisterDTO userRegisterDTO) {
        return this.save(UserConverter.INSTANCE.toEntity(userRegisterDTO));
    }

    @Override
    public void logout() {
        WebUtils.getRequest().getSession().invalidate();
    }

    @Override
    public PageInfo<User> getPage(Integer pIndex, Integer pSize, UserQueryDTO userQueryDTO) {
        PageHelper.startPage(pIndex, pSize);

        String username = userQueryDTO.getUsername();
        String phone = userQueryDTO.getPhone();

        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.like(StrUtil.isNotBlank(username), User::getUsername, username);
        lqw.like(StrUtil.isNotBlank(phone), User::getPhone, phone);

        lqw.orderByDesc(User::getCreateTime);

        List<User> users = baseMapper.selectList(lqw);

        return new PageInfo<>(users, 5);
    }

    @Override
    public User getByUsername(String username) {
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername, username);
        return this.getOne(lqw);
    }

    @Override
    public boolean save(User entity) {
        // 加密密码
        entity.setPassword(DigestUtil.md5Hex(entity.getPassword()));
        return super.save(entity);
    }
}




